<!DOCTYPE html>

<html>
<head>
  <title>baseModel.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="app.html">
                app.coffee
              </a>
            
              
              <a class="source" href="appInstance.html">
                appInstance.coffee
              </a>
            
              
              <a class="source" href="appMessages.html">
                appMessages.coffee
              </a>
            
              
              <a class="source" href="appSettings.html">
                appSettings.coffee
              </a>
            
              
              <a class="source" href="baseList.html">
                baseList.coffee
              </a>
            
              
              <a class="source" href="baseModel.html">
                baseModel.coffee
              </a>
            
              
              <a class="source" href="countdown.html">
                countdown.coffee
              </a>
            
              
              <a class="source" href="countdownList.html">
                countdownList.coffee
              </a>
            
              
              <a class="source" href="eggTimer.html">
                eggTimer.coffee
              </a>
            
              
              <a class="source" href="scheduledTwitter.html">
                scheduledTwitter.coffee
              </a>
            
              
              <a class="source" href="aboutView.html">
                aboutView.coffee
              </a>
            
              
              <a class="source" href="baseView.html">
                baseView.coffee
              </a>
            
              
              <a class="source" href="countdownChristmasView.html">
                countdownChristmasView.coffee
              </a>
            
              
              <a class="source" href="countdownView.html">
                countdownView.coffee
              </a>
            
              
              <a class="source" href="eggTimerView.html">
                eggTimerView.coffee
              </a>
            
              
              <a class="source" href="indexView.html">
                indexView.coffee
              </a>
            
              
              <a class="source" href="statsView.html">
                statsView.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>baseModel.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h2>BASE MODEL</h2>

            </div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>This serves as the base for all other models.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">BaseModel</span></span>

    id: <span class="literal">null</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Holds the URL used to load and save data. Loading will use a GET request,
and saving will use a POST request.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    url: <span class="literal">null</span></pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>Model&#39;s data are stored under the <code>attributes</code> property.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    attributes:
        creationDate: <span class="literal">null</span>     <span class="comment"># when was the model created?</span>
        creatorIP: <span class="literal">null</span>        <span class="comment"># the client IP which created the model</span>
        createdFrom: <span class="literal">null</span>      <span class="comment"># where was it originated from (dekstop, mobile phone id, email, twitter, etc)</span></pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>The constructor accepts a JSON object with the model&#39;s attributes.
If the <code>props</code> is an array, consider only its first index.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    constructor: (props) -&gt;
        <span class="keyword">return</span> <span class="keyword">if</span> <span class="keyword">not</span> props?

        <span class="keyword">if</span> props <span class="keyword">instanceof</span> Array
            <span class="property">@set</span> props[<span class="number">0</span>]
        <span class="keyword">else</span>
            <span class="property">@set</span> props</pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <h2>GENERAL HELPERS</h2>

            </div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>Helper method to get the object&#39;s type name.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getTypeName: =&gt;
        arr = <span class="property">@constructor</span>.toString().match(<span class="regexp">/function\s*(\w+)/</span>)
        <span class="keyword">return</span> arr[<span class="number">1</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Helper to get the ID of the model to be used as the key
on the browser&#39;s local storage.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getLocalId: =&gt;
        <span class="keyword">return</span> <span class="property">@getTypeName</span>() + <span class="string">"-"</span> + <span class="property">@id</span></pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Get the URL for saving and loading. Default <code>db/models_url</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getUrl: =&gt;
        <span class="keyword">return</span> <span class="string">"db/"</span> + <span class="property">@url</span></pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <h2>GET AND SET ATTRIBUTES</h2>

            </div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>Get a property value.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    get: (prop) =&gt;
        <span class="keyword">return</span> <span class="property">@attributes</span>[prop]</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p>Get a property value as boolean.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getBool: (prop) =&gt;
        result = <span class="property">@get</span> prop
        <span class="keyword">if</span> result <span class="keyword">is</span> <span class="literal">true</span> <span class="keyword">or</span> result <span class="keyword">is</span> <span class="number">1</span> <span class="keyword">or</span> result <span class="keyword">is</span> <span class="string">"true"</span>
            <span class="keyword">return</span> <span class="literal">true</span>
        <span class="keyword">else</span>
            <span class="keyword">return</span> <span class="literal">false</span></pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>Set a property value. If <code>prop</code> is an object, it will append all its
attributes to the current model.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    set: (prop, value, ignoreEmpty) =&gt;
        ignoreEmpty = <span class="literal">false</span> <span class="keyword">if</span> <span class="keyword">not</span> ignoreEmpty?

        <span class="keyword">if</span> <span class="keyword">typeof</span> prop <span class="keyword">is</span> <span class="string">"string"</span>
            <span class="keyword">if</span> (<span class="keyword">not</span> ignoreEmpty) <span class="keyword">or</span> (ignoreEmpty <span class="keyword">and</span> value? <span class="keyword">and</span> value <span class="keyword">isnt</span> <span class="string">""</span>)
                <span class="property">@attributes</span>[prop] = value
                <span class="property">@id</span> = value <span class="keyword">if</span> prop <span class="keyword">is</span> <span class="string">"id"</span>
        <span class="keyword">else</span> <span class="keyword">if</span> prop?
            <span class="property">@set</span> p, prop[p], ignoreEmpty <span class="keyword">for</span> own p <span class="keyword">of</span> prop</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>Delete the specified property.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    del: (prop) =&gt;
        <span class="keyword">delete</span> <span class="property">@attributes</span>[prop]</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <h2>SAVE DATA</h2>

            </div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <p>Save the model. It will first save it on the browser&#39;s local storage,
then send a PUT request to the server. If the server returns an OK
response, delete the saved data from the local storage.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    save: (options) =&gt;
        <span class="property">@saveLocal</span> options
        <span class="property">@saveRemote</span> options</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>Save the model on the browser&#39;s local storage.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    saveLocal: (options) =&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> window.localStorage?
            <span class="keyword">return</span>

        window.localStorage.setItem <span class="property">@getLocalId</span>(), <span class="property">@serialize</span>()</pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>Send the model to the remote server. If the model&#39;s ID is already specified,
do a PUT request and send the ID along the attributes, otherwise do a POST.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    saveRemote: (options) =&gt;
        data = <span class="property">@getAttributesForSaving</span>()

        <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@id</span>? <span class="keyword">or</span> <span class="property">@id</span> <span class="keyword">is</span> <span class="string">""</span>
            methodType = <span class="string">"POST"</span>
        <span class="keyword">else</span>
            methodType = <span class="string">"PUT"</span>
            data.id = <span class="property">@id</span>

        <span class="keyword">if</span> window.App.Settings.General.debug
            console.log <span class="string">"Model.saveRemote"</span>, methodType, data, options

        $.ajax
            error: (xhr, status, err) =&gt; <span class="property">@saveRemote_Error</span> options, err
            success: (data) =&gt; <span class="property">@saveRemote_Success</span> options, data
            data: data
            dataType: <span class="string">"json"</span>
            timeout: window.App.Settings.Sync.timeout
            type: methodType
            url: <span class="property">@getUrl</span>()</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>When remote saving is NOT successful, trigger the <code>onError</code> callback but
only if one was specified on the save options.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    saveRemote_Error: (options, err) =&gt;
        <span class="keyword">if</span> window.App.Settings.General.debug
            console.warn <span class="string">"Model.saveRemote"</span>, <span class="string">"Error"</span>, err, options

        <span class="keyword">if</span> options.onError?
            options.onError <span class="keyword">this</span>, err</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <p>When remote saving IS successful, trigger the <code>onSuccess</code> callback but
only if one was specified on the save options.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    saveRemote_Success: (options, data) =&gt;
        <span class="keyword">if</span> window.App.Settings.General.debug
            console.log <span class="string">"Model.saveRemote"</span>, <span class="string">"Success"</span>, data, options

        <span class="property">@id</span> = data.id
        <span class="property">@set</span> data

        <span class="keyword">if</span> options.onSuccess?
            options.onSuccess <span class="keyword">this</span>, data</pre></div></div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              <h2>LOAD DATA</h2>

            </div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-22">&#182;</a>
              </div>
              <p>Load the model&#39;s data and attributes. First check if there&#39;s an unsaved
model on the browser&#39;s local storage, and then fetch the information
from the MongoDB server.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    load: (options) =&gt;
        <span class="property">@loadRemote</span> options</pre></div></div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-23">&#182;</a>
              </div>
              <p>Load model data from the browser&#39;s local storage.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    loadLocal: (options) =&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> window.localStorage?
            <span class="keyword">return</span>

        window.localStorage.getitem <span class="property">@localId</span>()</pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-24">&#182;</a>
              </div>
              <p>Load model data from the remote MongoDB server.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    loadRemote: (options) =&gt;
        $.ajax
            error: (xhr, status, err) =&gt; <span class="property">@loadRemote_Error</span> options, err
            success: (data) =&gt; <span class="property">@loadRemote_Success</span> options, data
            dataType: <span class="string">"json"</span>
            timeout: window.App.Settings.Sync.timeout
            type: <span class="string">"GET"</span>
            url: <span class="property">@getUrl</span>()</pre></div></div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-25">&#182;</a>
              </div>
              <p>When remote loading is NOT successful, trigger the <code>onError</code> callback but
only if one was specified on the save options.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    loadRemote_Error: (options, err) =&gt;
        <span class="keyword">if</span> window.App.Settings.General.debug
            console.log <span class="string">"Model.loadRemote"</span>, <span class="string">"Error"</span>, err, options

        <span class="keyword">if</span> options.onError?
            options.onError <span class="keyword">this</span>, err</pre></div></div>
            
        </li>
        
        
        <li id="section-26">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-26">&#182;</a>
              </div>
              <p>When remote loading IS successful, trigger the <code>onSuccess</code> callback but
only if one was specified on the save options.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    loadRemote_Success: (options, data) =&gt;
        <span class="keyword">if</span> window.App.Settings.General.debug
            console.log <span class="string">"Model.loadRemote"</span>, <span class="string">"Success"</span>, data, options

        <span class="property">@id</span> = data.id
        <span class="property">@set</span> data

        <span class="keyword">if</span> options.onSuccess?
            options.onSuccess <span class="keyword">this</span>, data</pre></div></div>
            
        </li>
        
        
        <li id="section-27">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-27">&#182;</a>
              </div>
              <h2>CLEAR DATA</h2>

            </div>
            
        </li>
        
        
        <li id="section-28">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-28">&#182;</a>
              </div>
              <p>Clear the local storage copy of the model, if there&#39;s one.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    clearLocal: =&gt;
        window.localStorage.removeItem <span class="property">@getLocalId</span>()</pre></div></div>
            
        </li>
        
        
        <li id="section-29">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-29">&#182;</a>
              </div>
              <h2>TRANSFORM DATA</h2>

            </div>
            
        </li>
        
        
        <li id="section-30">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-30">&#182;</a>
              </div>
              <p>Get only valid attributes to save to the local storage or remote database.
Valid attributes are the one with values not being undefined, null or empty.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getAttributesForSaving: =&gt;
        result = {}
        <span class="keyword">for</span> key, value <span class="keyword">of</span> <span class="property">@attributes</span>
            result[key] = value <span class="keyword">if</span> value? <span class="keyword">and</span> value <span class="keyword">isnt</span> <span class="string">""</span>
        <span class="keyword">return</span> result</pre></div></div>
            
        </li>
        
        
        <li id="section-31">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-31">&#182;</a>
              </div>
              <p>Serialize the model to a JSON string passing its ID and attributes.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    serialize: =&gt;
        <span class="keyword">return</span>
        {
            id: <span class="property">@id</span>
            attributes: <span class="property">@getAttributesForSaving</span>()
        }</pre></div></div>
            
        </li>
        
        
        <li id="section-32">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-32">&#182;</a>
              </div>
              <p>Parse the specified data (string or object) and bind its attributes to the model.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    parse: (data) =&gt;
        <span class="property">@attributes</span> = data</pre></div></div>
            
        </li>
        
        
        <li id="section-33">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-33">&#182;</a>
              </div>
              <h2>MODULE EXPORTS</h2>

            </div>
            
        </li>
        
        
        <li id="section-34">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-34">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre><span class="keyword">if</span> exports?
    module.exports = BaseModel
<span class="keyword">else</span>
    window.App.BaseModel = BaseModel</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
